package com.edward.service;


import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.ExistentInvoice;
import com.edward.model.UnSubmit;


public class UnSubmitManager extends HibernateDaoSupport {

	public void addUnSubmit(UnSubmit unSubmit) {
		
		getHibernateTemplate().save(unSubmit);
	}
	
	public void updateUnSubmit(UnSubmit unSubmit) {
		
		getHibernateTemplate().update(unSubmit);
	}
	
	public void deleteUnSubmit(int id){
		
		getHibernateTemplate().delete(getUnSubmit(id));
	}
	
	public UnSubmit getUnSubmit(int id){
		UnSubmit d =(UnSubmit)getHibernateTemplate().get(UnSubmit.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<UnSubmit> listUnSubmit() {
		return getHibernateTemplate().find("from UnSubmit u where u.tag = 0");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <UnSubmit>list = getHibernateTemplate().find("from UnSubmit u where u.tag = 0" );
		UnSubmit u = new UnSubmit();
		u.setCheckCode("合计");
		u.setEsum(0.00);
		for (UnSubmit unSubmit : list) {
			u.setMoney(u.getMoney() + unSubmit.getMoney());
			if(unSubmit.getEsum() != null){
				u.setEsum(u.getEsum() + unSubmit.getEsum());
			}
		}
		int a = 0;
		double d ;
		a = (int) (u.getMoney() * 100);
		d = a;
		u.setMoney(d/100);
		a = (int) (u.getEsum() * 100);
		d = a;
		u.setEsum(d/100);
		list.add(u);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <UnSubmit>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	public void addEI(ExistentInvoice ei){
		ei.setFlag("t");
		getHibernateTemplate().save(ei);
	}
	
	
	
	public void updateEI(ExistentInvoice ei){
		ei.setFlag("t");
		getHibernateTemplate().update(ei);
	}
	
	
	public ExistentInvoice getEI(int id){
		ExistentInvoice ei =(ExistentInvoice)getHibernateTemplate().get(ExistentInvoice.class, id);
		return ei;
	}
	
	public void deleteEI(ExistentInvoice ei){
		ei.setUnSubmit(null);
		ei.setActualDate(null);
		ei.setFlag(null);
		ei.setNoticeDate(null);
		ei.setPromiseDate(null);
		ei.setReason(null);
		getHibernateTemplate().update(ei);
		
	}
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerEI(int start,int limit,int id) {
		List <ExistentInvoice>list = getHibernateTemplate().find("from ExistentInvoice e where SUBMIT_ID = " + id + "and e.flag = 't'" );
		if(list.size()>1){
			ExistentInvoice e = new ExistentInvoice();
			e.setName("合计");
			for (ExistentInvoice existentInvoice : list) {
				e.setMoney(e.getMoney() + existentInvoice.getMoney());
				e.setTax(e.getTax() + existentInvoice.getTax());
			}
			int a = 0;
			double d ;
			a = (int) (e.getMoney() * 100);
			d = a;
			e.setMoney(d/100);
			a = (int) (e.getTax() * 100);
			d = a;
			e.setTax(d/100);
			list.add(e);
		}
		
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ExistentInvoice>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")
	public PagerModel pagerEI(int start,int limit,String id[]) {
		List <ExistentInvoice>list = new ArrayList<ExistentInvoice>();
		int idd = 0;
		for (int i = 0; i < id.length; i++) {
		     idd = Integer.parseInt(id[i]);
			 list.addAll(getHibernateTemplate().find("from ExistentInvoice e where SUBMIT_ID = " + idd + "and e.flag = 't'" ));
		}
		ExistentInvoice e = new ExistentInvoice();
		e.setName("合计");
		for (ExistentInvoice existentInvoice : list) {
			e.setMoney(e.getMoney() + existentInvoice.getMoney());
			e.setTax(e.getTax() + existentInvoice.getTax());
		}
		int a = 0;
		double d ;
		a = (int) (e.getMoney() * 100);
		d = a;
		e.setMoney(d/100);
		a = (int) (e.getTax() * 100);
		d = a;
		e.setTax(d/100);
		list.add(e);
		
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ExistentInvoice>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<ExistentInvoice> getEIList(int id){
		List <ExistentInvoice>list = getHibernateTemplate().find("from ExistentInvoice e where SUBMIT_ID = " + id );
		return list;
		
	}
	



}



